//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.RegistryManagerModule
{
    using System.Collections.ObjectModel;
    using ManagementStudioServices.Interfaces;
    using Microsoft.Research.ScientificWorkflow.ManagementStudioModels;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using Microsoft.Practices.Unity;

    public class AssemblyCatalogueLeftPanePresenter : RegistryManagerLeftPaneItemPresenterBase
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="container">IUnityContainer</param>
        /// <param name="leftPane">AssemblyCatalogueLeftPane</param>
        /// <param name="service">IRegistryManagerService</param>
        public AssemblyCatalogueLeftPanePresenter(IUnityContainer container, AssemblyCatalogueLeftPane leftPane, IRegistryManagerService service)
            : base(container, service)
        {
            this.LeftPaneView = leftPane;
            leftPane.DataContext = this;
        }
        /// <summary>
        /// Gets or sets the root model.
        /// </summary>
        /// <value>The root model.</value>
        public override BaseRegMgrModel RootModel
        {
            get;
            set;
        }

        /// <summary>
        /// Intialise Left Pane Tree
        /// </summary>
        public override void Initialize()
        {
            try
            {
                this.LeftPaneTreeItems.Clear();
                this.RootModel = new AssemblyPackageViewModel(ManagementStudioResourceManager.GetString("AssembliesLeftPaneTitle"));
                ((AssemblyPackageViewModel)this.RootModel).AssignActivityIcon();
                Collection<AssemblyPackageViewModel> assemblyPackages = this.RegistryManagerService.GetAllAssemblyPackages();
                foreach (AssemblyPackageViewModel assemblyPackage in assemblyPackages)
                {
                    this.RootModel.Children.Add(assemblyPackage);
                    assemblyPackage.Parent = this.RootModel;
                }

                this.LeftPaneTreeItems.Add(this.RootModel);
            }
            catch (TridentCustomException ex)
            {
                TridentErrorHandler.HandleTridentCustomExceptionInUI(ex);
                TridentMessageBox.ShowTridentErrorMessageBox(ex.CustomMessage.ToString()); 
            }
        }
    }
}
